﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crude.Common;
using Crude.Core.Repository;

namespace Crude.VisualStudioAddin.Forms
{
    public partial class AddCrudeReferenceForm : Form
    {
        IList<string> m_selectedReferences;

        IList<string> m_existReferences;
        string m_projectName;

        public AddCrudeReferenceForm(string projectName, IList<string> existReferences)
        {
            InitializeComponent();
            m_projectName = projectName;
            m_existReferences = existReferences;
        }

        public IList<string> SelectedReferences
        {
            get
            {
                return m_selectedReferences;
            }
        }

        private void AddCrudeReferenceForm_Load(object sender, EventArgs e)
        {
            IRemoteRepository remoteRepository = new RemoteRepositoryBuilder().Build();

            IList<string> availableReferences = remoteRepository.GetAllProjectNames();
 
            availableReferences.Remove(m_projectName);

            foreach (string existProjectReference in m_existReferences)
	        {
                availableReferences.Remove(existProjectReference);
	        }

            listBox1.DataSource = availableReferences.OrderBy(p => p).ToList() ;            
        }

        private void m_cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void m_okButton_Click(object sender, EventArgs e)
        {
            m_selectedReferences = (
                from int index in listBox1.SelectedIndices
                select (string)listBox1.Items[index]).ToList();

            DialogResult = DialogResult.OK;
            Close();
        }


    }
}
